(function () {
    var map,baseLayer;


    //定义图层样式
    var mystyle = new ol.style.Style({
        fill: new ol.style.Fill({
            color:"rgba(72,61,139, 1)",//重点在这里，采用rgba颜色，最后一个值用来表示透明度
        }),
        stroke: new ol.style.Stroke({
            color:"#BDBDBD",
            width:2
        })
    })
    var vectorSource = new ol.source.Vector();
    var vectorLayer = new ol.layer.Vector({
        source: vectorSource,
        style: mystyle
    })
    /**
     * 初始化地图
     */
    function initMap(){


         baselayer = new ol.layer.Tile({
            source: new ol.source.XYZ({
                url: 'https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetGray/MapServer/tile/{z}/{y}/{x}'
            })
        });
        // var view = new ol.View({
        //     center: [116.727085860608, 35.20619600133295],
        //     zoom:10.5,
        //     projection: "EPSG:4326"
        // });

        // map = new ol.Map({
        //     target: "map",
        //     view: view,
        //     layers:[baseLayer,vectorLayer]
        // });
        map = new ol.Map({
            target: 'map',
            layers: [baselayer],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [112, 36],
                zoom: 6
            })
        });

        map.addLayer(vectorLayer);
    }

    /**
     * 根据名称加载遮罩层
     * @param {*} name1
     */
    function addCoverLayer(name1){
        //清除原来的要素
        vectorSource.clear();

        // $.getJSON('./data/shanxi.json',function(data){
        $.getJSON('./data/SZLY00000002.json',function(data){
            var features = (new ol.format.GeoJSON()).readFeatures(data);
            features.forEach(function(element) {
                //从数据中取出name字段值为name1的区域,进行加载
                // if(element.get("name") === name1){
                    vectorSource.addFeature(element);
                // }
            });
        })

        // $.getJSON('./data/SZLY00000001.json',function(data){
        //     var features = (new ol.format.GeoJSON()).readFeatures(data);
        //     var tempfeatures=features[0];
        //     tempfeatures.forEach(function(element) {
        //         //从数据中取出name字段值为name1的区域,进行加载
        //         // if(element.get("name") === name1){
        //         vectorSource.addFeature(element);
        //         // }
        //     });
        // })

        // $.getJSON('./data/SZLY00000001.json',function(data){
        //     var features = (new ol.format.GeoJSON()).readFeatures(data);
        //     features.forEach(function(element) {
        //         //从数据中取出name字段值为name1的区域,进行加载
        //         // if(element.get("name") === name1){
        //         vectorSource.addFeature(element);
        //         // }
        //     });
        // })
    }

    initMap();
    addCoverLayer();
})();